package com.example.hotsearchplatform.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.hotsearchplatform.dto.HotSearchForward5DTO;
import com.example.hotsearchplatform.dto.HotTopDifferentDTO;
import com.example.hotsearchplatform.dto.HotValueByPlatformDTO;
import com.example.hotsearchplatform.mapper.AnalysisMapper;
import com.example.hotsearchplatform.pojo.entity.Analysis;
import com.example.hotsearchplatform.service.AnalysisService;
import com.example.hotsearchplatform.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.example.hotsearchplatform.mapper.AnalysisMapper;
import com.example.hotsearchplatform.pojo.entity.Analysis;
import com.example.hotsearchplatform.service.AnalysisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class AnalysisServiceImpl extends ServiceImpl<AnalysisMapper, Analysis> implements AnalysisService {
    @Autowired
    private AnalysisMapper analysisMapper;

    /**
     * 不同平台每日关键字热搜总次数
     * @return
     */
    @Override
    public List<searchCountVO> getSearchCount() {

        List<searchCountVO> searchCountVOS = new ArrayList<>();
        List<searchCountVO> weiboVOS = new ArrayList<>();
        List<searchCountVO> douyinVOS = new ArrayList<>();
        List<searchCountVO> kuaishouVOS = new ArrayList<>();
        List<searchCountVO> baiduVOS = new ArrayList<>();
        List<searchCountVO> zhihuVOS = new ArrayList<>();
        Map<String, List<Integer>> platformDataMap = new HashMap<>();

        List<platformVO> list = analysisMapper.getSearchCount();

        for (platformVO plf : list) {
            String platformName = plf.getName();
            int data = plf.getData();

            // 如果map中还没有该平台的键，则初始化一个list
            platformDataMap.putIfAbsent(platformName, new ArrayList<>());

            // 将当前平台的数据添加到对应的list中
            platformDataMap.get(platformName).add(data);
        }

        for (Map.Entry<String, List<Integer>> entry : platformDataMap.entrySet()) {
            String name = entry.getKey();
            List<Integer> data = entry.getValue();

            searchCountVO vo = new searchCountVO();
            vo.setName(name);
            vo.setType("line");
            vo.setStack("Total");
            vo.setData(data);

            switch (name) {
                case "微博":
                    weiboVOS.add(vo);
                    break;
                case "抖音":
                    douyinVOS.add(vo);
                    break;
                case "快手":
                    kuaishouVOS.add(vo);
                    break;
                case "百度":
                    baiduVOS.add(vo);
                    break;
                case "知乎":
                    zhihuVOS.add(vo);
                    break;
                default:
                    // 处理其他未列出的平台情况，或者忽略
                    break;
            }
        }

        searchCountVOS.addAll(weiboVOS);
        searchCountVOS.addAll(douyinVOS);
        searchCountVOS.addAll(kuaishouVOS);
        searchCountVOS.addAll(baiduVOS);
        searchCountVOS.addAll(zhihuVOS);
        return searchCountVOS;
    }

    @Override
    public List<HotValueByPlatformDTO> getPieChart() {
        List<HotValueByPlatformDTO> hotValueByPlatformDTOS = analysisMapper.selectTotalHotTopGroupedByPlatform();
        return hotValueByPlatformDTOS;
    }



    @Override
    public List<WordCloudVO> getAllWorldInfo(String platform) {
        List<WordCloudVO> wordCloudVOList=analysisMapper.getAllWordCloud(platform);
        return wordCloudVOList;
    }

    @Override
    public List<KeyWordListAndTopListVO> getHotTopByDifferentDayInfo(HotTopDifferentDTO hotTopDifferentDTO) {
        List<Map<String, Object>> resultMapList = analysisMapper.getHotTopByDifferentDay(hotTopDifferentDTO);

        return convertToKeyWordListAndTopListVOList(resultMapList);
    }

    private List<KeyWordListAndTopListVO> convertToKeyWordListAndTopListVOList(List<Map<String, Object>> resultMapList) {
        return resultMapList.stream().map(resultMap -> {
            String hotKeyWordListStr = (String) resultMap.get("hotKeyWordList");
            String hotTopListStr = (String) resultMap.get("hotTopList");


            List<String> hotKeyWordList = Arrays.asList(hotKeyWordListStr.split(","));
            List<String> hotTopList = Arrays.asList(hotTopListStr.split(","));

            KeyWordListAndTopListVO vo = new KeyWordListAndTopListVO();
            vo.setHotKeyWordList(hotKeyWordList);
            vo.setHotTopList(hotTopList);
            return vo;
        }).collect(Collectors.toList());
    }


}
